/*
 * Copyright (c) 2007, 2008 fr3@K <freak@fsfoundry.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef GION__AUTO_ARRAY_HPP
#define GION__AUTO_ARRAY_HPP

#include <cstddef> // std::size_t
#include <algorithm> // std::swap
#include <cassert>

namespace gion
{

  namespace bits
  {
    template <class T>
    struct auto_array_ref
    {
      typedef T element_type;

      explicit auto_array_ref(element_type* array)
        : array(array)
      {}
    
      element_type* array;
    };
  } // namespace bits

  template <class T>
  class auto_array
  {
  public:
    typedef T element_type;

    explicit auto_array(
      element_type* array = 0)
      : array_(array)
    {
    }

    auto_array(auto_array& other)
      : array_(other.release())
    {
    }
    template <class Other>
    auto_array(auto_array<Other>& other)
      : array_(other.release())
    {
    }

    auto_array& operator=(auto_array& other)
    {
      this->reset(other.release());
      return *this;
    }
    template <class Other>
    auto_array& operator=(auto_array<Other>& other)
    {
      this->reset(other.release());
      return *this;
    }

    auto_array(bits::auto_array_ref<element_type> ref)
      : array_(ref.array)
    {
    }
    auto_array& operator=(bits::auto_array_ref<element_type> ref)
    {
      this->reset(ref.array);
      return *this;
    }
    template <class Other>
    auto_array& operator=(bits::auto_array_ref<Other> ref)
    {
      this->reset(ref.array);
      return *this;
    }

    ~auto_array()
    {
      this->reset();
    }

    operator bits::auto_array_ref<element_type>()
    {
      return bits::auto_array_ref<element_type>(this->release());
    }

    template <class Other>
    operator bits::auto_array_ref<Other>()
    {
      return bits::auto_array_ref<Other>(this->release());
    }

    element_type& operator[](std::size_t offset) const
    {
      return array_[offset];
    }

    element_type* get() const
    {
      return array_;
    }
    element_type* release()
    {
      element_type* ret(array_);
      array_ = 0;
      return ret;
    }
    void reset(element_type* array = 0)
    {
      if(array_ != 0)
      {
        assert(array_ != array);
        delete [] this->release();
      }
      array_ = array;
    }

    void swap(auto_array& other)
    {
      using std::swap;
      swap(array_, other.array_);
    }

  private:
    element_type* array_;
  };

  template <class T>
  void swap(
    auto_array<T>& lhs,
    auto_array<T>& rhs)
  {
    lhs.swap(rhs);
  }

} // namespace gion

#endif // !GION__AUTO_ARRAY_HPP
